package com.practice.algorithm.slidingWindow;

import java.util.HashMap;
import java.util.Map;

public class Main {
    static class Solution {
        // 有一个贪心的思想，长度越长肯定越满足
        public long countGood(int[] nums, int k) {
            long ans = 0;
            Map<Integer,Integer> cnt = new HashMap<>();
            int pairs = 0;
            int left =0;
            for(int x : nums){
                int c = cnt.getOrDefault(x,0);
                pairs += c;
                cnt.put(x,c+1);
                while(pairs >= k){
                    x = nums[left];
                    c = cnt.get(x);
                    pairs -= c-1;
                    cnt.put(x,c-1);
                    left++;
                }
                ans += left;
            }
            return ans;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = {3,1,4,3,2,2,4};
        System.out.println(s.countGood(nums,2));
    }
}
